package com.anxin.registerManagement.service;

import com.alibaba.fastjson.JSONObject;
import com.anxin.registerManagement.bean.dto.AreaDto;
import com.anxin.registerManagement.bean.dto.CodeDto;
import com.anxin.registerManagement.bean.persist.code.area.CodeCounty;
import com.anxin.registerManagement.bean.persist.code.area.CodeMunicipal;
import com.anxin.registerManagement.bean.persist.code.area.CodeProvince;
import com.anxin.registerManagement.mapper.code.CodeCulturalLevelMapper;
import com.anxin.registerManagement.mapper.code.CodeNationalityMapper;
import com.anxin.registerManagement.mapper.code.CodePoliticalStatusMapper;
import com.anxin.registerManagement.mapper.code.CodeRoleNameMapper;
import com.anxin.registerManagement.mapper.code.area.CodeCountyMapper;
import com.anxin.registerManagement.mapper.code.area.CodeMunicipalMapper;
import com.anxin.registerManagement.mapper.code.area.CodeProvinceMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;

@Service
@EnableScheduling
public class CodeService {

    private Logger logger = LoggerFactory.getLogger(CodeService.class);

    @Autowired
    CodeCulturalLevelMapper codeCulturalLevelMapper;

    @Autowired
    CodeNationalityMapper codeNationalityMapper;

    @Autowired
    CodePoliticalStatusMapper codePoliticalStatusMapper;

    @Autowired
    CodeRoleNameMapper codeRoleNameMapper;

    @Autowired
    private CodeCountyMapper codeCountyMapper;

    @Autowired
    private CodeMunicipalMapper codeMunicipalMapper;

    @Autowired
    private CodeProvinceMapper codeProvinceMapper;

    private CodeDto codeDto = new CodeDto();

    /**
     * 在服务器加载时执行，并只执行一次
     */
    @PostConstruct
    public void init() {
        lodingCode();
    }

    /**
     * 获取所有code信息
     */
    public CodeDto getCodeList() {
        return codeDto;
    }

    /**
     * 定时加载code列表
     */
    @Scheduled(cron = "0 0/1 * * * ?")
    public void lodingCode() {
        CodeDto codeDto = new CodeDto();
        codeDto.setCulturalLevelList(codeCulturalLevelMapper.selectList(null));
        codeDto.setNationalityList(codeNationalityMapper.selectList(null));
        codeDto.setPoliticalStatusList(codePoliticalStatusMapper.selectList(null));
        codeDto.setRoleNameList(codeRoleNameMapper.selectList(null));
        codeDto.setAreaList(getAreaList());
        this.codeDto = codeDto;
    }

    public List<AreaDto> getAreaList() {
        List<CodeProvince> codeProvinces = codeProvinceMapper.selectList(null);

        List<AreaDto> provincesList = new ArrayList<>();
        for (CodeProvince codeProvince : codeProvinces) {
            // 通过省份编码查询所有市级数据
            LambdaQueryWrapper<CodeMunicipal> codeMunicipalLambdaQueryWrapper = new LambdaQueryWrapper<>();
            codeMunicipalLambdaQueryWrapper.eq(CodeMunicipal::getProvinceCode, codeProvince.getCode());
            List<CodeMunicipal> codeMunicipals = codeMunicipalMapper.selectList(codeMunicipalLambdaQueryWrapper);
            List<JSONObject> municipalsList = new ArrayList<>();
            for (CodeMunicipal codeMunicipal : codeMunicipals) {
                // 通过市级编码查询所有区级数据
                LambdaQueryWrapper<CodeCounty> codeCountyLambdaQueryWrapper = new LambdaQueryWrapper<>();
                codeCountyLambdaQueryWrapper.eq(CodeCounty::getMunicipalCode, codeMunicipal.getCode());
                List<CodeCounty> codeCounties = codeCountyMapper.selectList(codeCountyLambdaQueryWrapper);

                List<JSONObject> countiesList = new ArrayList<>();
                for (CodeCounty codeCounty : codeCounties) {
                    JSONObject countyJSONObject = new JSONObject();
                    countyJSONObject.put("code", codeCounty.getCode());
                    countyJSONObject.put("value", codeCounty.getValue());
                    countiesList.add(countyJSONObject);
                }

                JSONObject municipalJSONObject = new JSONObject();
                municipalJSONObject.put("code", codeMunicipal.getCode());
                municipalJSONObject.put("value", codeMunicipal.getValue());
                municipalJSONObject.put("children", countiesList);
                municipalsList.add(municipalJSONObject);
            }

            AreaDto areaDto = new AreaDto();
            areaDto.setCode(codeProvince.getCode());
            areaDto.setValue(codeProvince.getValue());
            areaDto.setChildren(municipalsList);
            provincesList.add(areaDto);
        }
        return provincesList;
    }
}
